package cn.bld.controller;

import cn.bld.entity.StorageRecord;
import cn.bld.entity.SysRole;
import cn.bld.entity.SysUser;
import cn.bld.service.SysRoleService;
import cn.bld.utils.MyFileUtil;
import com.alibaba.fastjson.JSON;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/role")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;
    @GetMapping("/simpleList")
    @ResponseBody
    public String findRoles(){
        return JSON.toJSONString(sysRoleService.findall());
    }
    @GetMapping("page")
    public  String page( Model model){
        model.addAttribute("roleList",sysRoleService.findall());
        return "sysRole/list";
    }
    @GetMapping("/add")
    public String add(){
        return "sysRole/add";
    }
    @GetMapping("/roleExist")
    @ResponseBody
    public String checkCode(String code){
        int i = 1;
        Map<String,Object> map = new HashMap<>();
        try{
            if (StringUtils.isNullOrEmpty(code)){
                map.put("exist",-1);
                map.put("msg","用户名不能为空");
                return JSON.toJSONString(map);
            }
            i = sysRoleService.checkRole(code);
            if (i==0){
                map.put("exist",0);
            }else {
                map.put("exist",1);
            }
        }catch (Exception e){
            e.printStackTrace();
            map.put("exist",-1);
            map.put("msg","服务器访问异常，请稍后重试");
        }
        return JSON.toJSONString(map);
    }
    @PostMapping("toAdd")
    public String addRole(SysRole sysRole, HttpSession session) throws IOException {
        SysUser loginUser = (SysUser) session.getAttribute("userSession");
        sysRole.setCreatedUserId(loginUser.getId());
        int i = sysRoleService.insertRole(sysRole);
        if (i==1){
            return "redirect:/role/page";
        }
        return "redirect:/WEB-INF/jsp/sysRole/add.jsp";
    }
    @GetMapping("/view/{opr}/{id}")
    public String view(@PathVariable Integer id, Model model, @PathVariable String opr){
        model.addAttribute(("sysRole"),sysRoleService.findById(id));
        if("v".equals(opr)){
            return "/sysRole/view";
        }else {
            return "/sysRole/update";
        }

    }
    @PostMapping("update")
    public String update( SysRole sysRole, HttpSession session){
        SysUser loginUser = (SysUser) session.getAttribute("userSession");
        sysRole.setUpdatedUserId(loginUser.getId());
        int i = sysRoleService.updateSysRole(sysRole);
        return "redirect:/role/page";
    }


    @DeleteMapping("del/{roleid}")
    @ResponseBody
    public String del(@PathVariable Integer roleid){
        Map<String,Object> map = new HashMap<>();
        if (sysRoleService.findById(roleid)==null){
            map.put("delResult","notexist");
            return JSON.toJSONString(map);
        }
        int i = sysRoleService.deleteSysRole(roleid);
        if (i==1){
            map.put("delResult","true");
        }else {
            map.put("delResult","false");
        }
        return JSON.toJSONString(map);
    }
}
